package com.usian.article.service.impl;

import com.baomidou.mybatisplus.core.toolkit.Wrappers;
import com.usian.article.mapper.ApAuthorMapper;
import com.usian.article.mapper.ArticleConfigMapper;
import com.usian.article.mapper.ArticleContentMapper;
import com.usian.article.mapper.ArticleMapper;
import com.usian.article.service.ArticleService;
import com.usian.model.article.*;
import com.usian.model.base.BaseServiceImpl;
import com.usian.model.common.dtos.ResponseResult;
import com.usian.model.media.pojos.WmNews;
import com.usian.model.media.vos.WmNewsVo;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.Date;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

@Service
public class ArticleServiceImpl extends BaseServiceImpl<ArticleMapper, ApArticle> implements ArticleService {
    @Autowired
    private ApAuthorMapper authorMapper;
    @Autowired
    private ArticleContentMapper contentMapper;
    @Autowired
    private ArticleConfigMapper configMapper;
    @Override
    public ApArticle saveArticleInfo(WmNewsVo news) {
        if (news == null)return null;
        //保存文章信息
        ApArticle article = new ApArticle();
        article.setTitle(news.getTitle());
        Integer wmUserId = news.getUserId();
        ApAuthor author = authorMapper.selectOne(Wrappers.<ApAuthor>lambdaQuery().eq(ApAuthor::getWmUserId, wmUserId));
        article.setAuthorId(author.getId());
        article.setAuthorName(author.getName());
        article.setChannelId(news.getChannelId());
        article.setChannelName(news.channelName);
        article.setLayout(news.getType());
        article.setFlag((byte) 0);
        article.setImages(news.getImages());
        article.setLikes(0);
        article.setCollection(0);
        article.setComment(0);
        article.setViews(0);
        article.setCreatedTime(new Date());
        article.setPublishTime(news.getPublishTime());
        save(article);
        //保存文章详情
        ApArticleContent content = new ApArticleContent();
        content.setArticleId(article.getId());
        content.setContent(news.getContent());
        contentMapper.insert(content);
        //保存文章配置信息
        ApArticleConfig config = new ApArticleConfig();
        config.setArticleId(article.getId());
        config.setIsForward(false);
        config.setIsDown(false);
        config.setIsDelete(false);
        config.setIsComment(false);
        configMapper.insert(config);
        return article;
    }

    @Override
    public ResponseResult load(ArticleHomeDto dto) {
        if (dto == null ||dto.getLoadType() == null)return paramError;
        List<ApArticle> list = baseMapper.loadArticleList(dto);
        return success(list);
    }

    @Override
    public ResponseResult loadContent(Long articleId) {
        if (articleId == null)return paramError;
        ApArticleConfig config = configMapper.selectOne(Wrappers.<ApArticleConfig>lambdaQuery().eq(ApArticleConfig::getArticleId, articleId));
        Map<String,Object> resultMap = new HashMap<>();
        resultMap.put("config",config);
        if (!config.getIsDelete() && !config.getIsDown()){
            ApArticleContent content = contentMapper.selectOne(Wrappers.<ApArticleContent>lambdaQuery().eq(ApArticleContent::getArticleId, articleId));
            resultMap.put("content",content);
        }
        return success(resultMap);
    }
}
